#! /bin/bash
# support for GRUB version 2

error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code

set -a

# do not set up grub during dirinstall
if [ "$FAI_ACTION" = "dirinstall" ] ; then
    exit 0
fi
# during softupdate use this file
[ -r $LOGDIR/disk_var.sh ] && . $LOGDIR/disk_var.sh

if [ -z "$BOOT_DEVICE" ]; then
    exit 189
fi

# vsyscall=none can not work with centos 5.x and 6.x which shoud be vsyscall=emulate
# nosplash and ethX
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT.*$/GRUB_CMDLINE_LINUX_DEFAULT="quiet loglevel=3 selinux=0 rd.systemd.show_status=auto rd.udev.log_priority=3 scsi_mod.use_blk_mq=1 crashkernel=384M-2G:64M,2G-:128M workqueue.power_efficient=0 domain init_on_free=0, init_on_alloc=0 cma=128M vsyscall=none nosoftlockup mce=off threadirqs noresume nmi_watchdog=0 vt.cur_default=50 transparent_hugepage=never cgroup_memory=nokmem swapaccount=1 nf_conntrack.acct=0 log_buf_len=2M intel_pstate=disable processor.max_cstate=0 intel_idle.max_cstate=0 thermal.off=1 pcie_aspm=off cpufreq.off=1 idle=halt net.ifnames=0 biosdevname=0 mitigations=off noibrs nopti l1tf=off nomodeset nospec_store_bypass_disable noibpb spectre_v2_user=off spectre_v2=off kvm-intel.vmentry_l1d_flush=never no_timer_check noreplace-smp kvm-intel.nested=1 nosync percpu_alloc=page iommu.strict=0 intel_iommu=on amd_iommu=on video=efifb:off cryptomgr.notests rcu_nocbs=0-64 rcupdate.rcu_expedited=1 rcutree.kthread_prio=2 skew_tick=1"/' $target/etc/default/grub
sed -i s/^GRUB_HIDDEN_TIMEOUT/#GRUB_HIDDEN_TIMEOUT/ $target/etc/default/grub
sed -i 's/^#GRUB_GFXMODE.*$/GRUB_GFXMODE=1024x768/' $target/etc/default/grub
ainsl /etc/default/grub 'GRUB_GFXPAYLOAD_LINUX="$GRUB_GFXMODE"'

# disable os-prober because of #788062
ainsl /etc/default/grub 'GRUB_DISABLE_OS_PROBER=true'

ainsl /etc/grub.d/40_custom 'set menu_color_normal=cyan/blue'
ainsl /etc/grub.d/40_custom 'set menu_color_highlight=white/blue'

# skip the rest, if not an initial installation
if [ $FAI_ACTION != "install" ]; then
    $ROOTCMD update-grub
    exit $error
fi

$ROOTCMD grub-mkdevicemap --no-floppy
GROOT=$($ROOTCMD grub-probe -tdrive -d $BOOT_DEVICE)


# Check if RAID is used for the boot device
if [[ $BOOT_DEVICE =~ '/dev/md' ]]; then
    raiddev=${BOOT_DEVICE#/dev/}
    # install grub on all members of RAID
    for device in `LC_ALL=C perl -ne 'if(/^'$raiddev'\s.+raid\d+\s(.+)/){ $_=$1; s/\d+\[\d+\]//g; print }' /proc/mdstat`; do
	echo Install grub on /dev/$device
	$ROOTCMD grub-install --no-floppy "/dev/$device"
    done

elif [[ $GROOT =~ 'hostdisk' ]]; then
    cat > $target/boot/grub/device.map <<EOF
(hd0)   $BOOT_DEVICE
EOF
    $ROOTCMD grub-install --no-floppy --modules=part_msdos $BOOT_DEVICE
    if [ $? -eq 0 ]; then
        echo "Grub installed on hostdisk $BOOT_DEVICE"
    fi
    rm $target/boot/grub/device.map

else
    $ROOTCMD grub-install --no-floppy "$GROOT"
    if [ $? -eq 0 ]; then
        echo "Grub installed on $BOOT_DEVICE = $GROOT"
    fi
fi
$ROOTCMD update-grub
# DOC https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html

exit $error
